package com.xingchi.tornado.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xingchi.tornado.mybatisplus.service.AbstractBasePlusService;
import com.xingchi.tornado.test.mapper.UserMapper;
import com.xingchi.tornado.test.model.domain.UserDO;
import com.xingchi.tornado.test.model.dto.UserDTO;
import com.xingchi.tornado.test.model.dto.UserQueryDTO;
import com.xingchi.tornado.test.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xingchi
 * @date 2025-03-10 14:08
 */
@Service
public class UserServiceImpl extends AbstractBasePlusService<UserMapper, UserDTO, UserQueryDTO, UserDO> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Wrapper<UserDO> getWrapper(UserQueryDTO query) {
        return Wrappers.<UserDO>lambdaQuery()
                .eq(StringUtils.isNotBlank(query.getUsername()), UserDO::getUsername, query.getUsername())
                .eq(StringUtils.isNotBlank(query.getMobile()), UserDO::getMobile, query.getMobile())
                .eq(StringUtils.isNotBlank(query.getEmail()), UserDO::getEmail, query.getEmail())
                .eq(query.getStatus() != null, UserDO::getStatus, query.getStatus());
    }

    @Override
    public Long create(UserDTO userDTO) {
        String password = userDTO.getPassword();
        String encode = passwordEncoder.encode(password);
        userDTO.setPassword(encode);
        UserDTO insert = this.insert(userDTO);
        return insert.getId();
    }

}
